package com.gtw.modules.api.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.annotation.AuthIgnoreApi;
import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.dto.TbCharmvalueNotesDto;
import com.gtw.dto.TbOrderDto;
import com.gtw.dto.TdAddressDto;
import com.gtw.modules.api.service.*;
import com.gtw.modules.base.service.TbCityService;
import com.gtw.modules.base.service.TbDistrictService;
import com.gtw.modules.base.service.TbProvinecService;
import com.gtw.modules.dao.TbAddressDao;
import com.gtw.modules.entity.*;
import com.gtw.modules.sys.service.TbIntegralService;
import com.gtw.utils.EntityDtoConverter;
import com.gtw.utils.JSONUtils;
import net.sf.json.JSONArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 会员魅力值记录
 *
 * @author wuye
 * @email www.baidu.com
 * @date 2018-02-06 15:19:47
 */
@RestController
@RequestMapping("/api/charmvaluenotes")
public class TbCharmvalueNotesController {
    @Autowired
    private TbCharmvalueNotesService charmvalueNotesService;

    @Autowired
    private TbIntegralService tbIntegralService;

    @Autowired
    private TbCommodityService tbCommodityService;
    @Autowired
    private TbOrderService tbOrderService;
    @Autowired
    private TbShoppingCartServices tbShoppingCartServices;

    @Autowired
    private TbAddressService tbAddressService;

    @Autowired
    private TbProvinecService tbProvinecService;

    @Autowired
    private TbCityService tbCityService;

    @Autowired
    private TbDistrictService tbDistrictService;

    /**
     * 查询列表
     *
     * @param params
     * @returnapi
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, @LoginUserApi TbUser tbUser) {
        params.put("uid", tbUser.getUuid());
        //查询列表数据
        Query query = new Query(params);
        Page<TbCharmvalueNotes> pageUtil = new Page<TbCharmvalueNotes>(query.getPage(), query.getLimit());
        Page<TbCharmvalueNotes> page = charmvalueNotesService.queryPageList(pageUtil, query);
        EntityDtoConverter edc = new EntityDtoConverter();
        Page<TbCharmvalueNotesDto> tbCharmvalueNotesDtoPage = new Page<TbCharmvalueNotesDto>(query.getPage(), query.getLimit());
        List<TbCharmvalueNotesDto> tbCharmvalueNotesDtoList = new ArrayList<>();
        for (int i = 0; i < page.getRecords().size(); i++) {
            TbCharmvalueNotesDto tbCharmvalueNotesDto = new TbCharmvalueNotesDto();
            try {
                tbCharmvalueNotesDto = (TbCharmvalueNotesDto) edc.entityConvertDto(page.getRecords().get(i), tbCharmvalueNotesDto);
                TbOrderDto tbOrderDto = new TbOrderDto();
                TbOrder tbOrder = tbOrderService.queryById(tbCharmvalueNotesDto.getOrderid());
//                tbOrderDto = (TbOrderDto) edc.entityConvertDto(tbOrder, tbOrderDto);
                if(tbOrder == null){

                }else {
                    tbOrderDto.setUsername(tbOrder.getUsername());
                    tbOrderDto.setUpdateuser(tbOrder.getUpdateuser());
                    tbOrderDto.setUpdatedate(tbOrder.getUpdatedate());
                    tbOrderDto.setTotalfee(tbOrder.getTotalfee());
                    tbOrderDto.setUid(tbOrder.getUid());
                    TbAddress tbAddress = tbAddressService.queryObject(tbOrder.getAddressid());
                    TbDistrict tbDistrict = tbDistrictService.findByDistrictId(tbAddress.getDistrictid());
                    TbProvinec tbProvinec = tbProvinecService.queryObject(tbAddress.getProvinceid());
                    TbCity tbCity = tbCityService.findByCityId(tbAddress.getCityid());
                    TdAddressDto dto = new TdAddressDto();
                    dto.setDistrictname(tbDistrict.getDistrictname());
                    dto.setDistrictid(tbDistrict.getDistrictid());
                    dto.setUid(tbAddress.getUid());
                    dto.setProvincename(tbProvinec.getProvincename());
                    dto.setProvinceid(tbProvinec.getProvinceid());
                    dto.setCityname(tbCity.getCityname());
                    dto.setCityid(tbCity.getCityid());
                    dto.setUsername(tbAddress.getUsername());
                    dto.setPostcode(tbAddress.getPostcode());
                    dto.setPhone(tbAddress.getPhone());
                    dto.setDetails(tbAddress.getDetails());
                    dto.setIsdefault(tbAddress.getIsdefault());

                    tbOrderDto.setTbAddressDto(dto);
                    tbOrderDto.setRemark(tbOrder.getRemark());
                    tbOrderDto.setPaytype(tbOrder.getPaytype());
                    tbOrderDto.setPaydate(tbOrder.getPaydate());
                    tbOrderDto.setCreatedate(tbOrder.getCreatedate());
                    tbOrderDto.setChannel(tbOrder.getChannel());
                    tbOrderDto.setBillno(tbOrder.getBillno());
                    //查询商品详细信息
                    JSONArray ja = JSONUtils.toJSONArray(tbOrder.getShoppingcartid());
                    List<TbShoppingCart> tbShoppingCartList = new ArrayList<>();
                    for (int j = 0; j < ja.size(); j++) {
                        tbShoppingCartList.add(tbShoppingCartServices.selectByPrimaryKey(ja.getInt(j)));
                    }
                    tbOrderDto.setTbShoppingCartList(tbShoppingCartList);
                }
                tbCharmvalueNotesDto.setTbOrderDto(tbOrderDto);
                tbCharmvalueNotesDtoList.add(tbCharmvalueNotesDto);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        tbCharmvalueNotesDtoPage.setRecords(tbCharmvalueNotesDtoList);
        return R.ok().put("data", tbCharmvalueNotesDtoPage);
    }


    /**
     * 等级
     */
    @RequestMapping("/level")
    public R level(int type) {
        List<TbIntegral> tbIntegralList = tbIntegralService.findAll(type);
        return R.ok().put("data", tbIntegralList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info")
    public R info(Integer id) {
        TbCharmvalueNotes charmvalueNotes = charmvalueNotesService.queryObject(id);

        return R.ok().put("charmvalueNotes", charmvalueNotes);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TbCharmvalueNotes charmvalueNotes) {
        charmvalueNotesService.save(charmvalueNotes);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TbCharmvalueNotes charmvalueNotes) {
        charmvalueNotesService.update(charmvalueNotes);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        charmvalueNotesService.delete(id);

        return R.ok();
    }

}
